/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.nyquistmeteocal.beans;

import it.nyquistmeteocal.entity.Event;
import it.nyquistmeteocal.boundary.EventManager;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
/**
 *
 * @author gatb27
 */
@Named(value = "eventdata")
@RequestScoped
public class EventData implements Serializable {

    private static final long serialVersionUID = 1L;
    
    @EJB
    private EventManager em;

    private List<Event> eventList;

    public List<Event> getEventList() {
        return eventList;
    }

    public void setEventList(List<Event> eventList) {
        this.eventList = eventList;
    }
    
    @PostConstruct
    public void init() {
        //try {
            eventList = em.getEvents();
        
    }

    /* public List<Event> getEvents() throws SQLException {
        ResultSet rs = null;
        PreparedStatement pst = null;
        Connection con = getConnection();

        System.out.println("value of LogUser before the get:   " + LogUser);
        LogUser = um.getLoggedUser().getEmail();
        System.out.println("value of LogUser after the get:   " + LogUser);

        sqlQuery = "SELECT event.name, event.place, event.eventdate, event.type, event.visibility, event.creator, event.id "
                + "FROM event, calendarevents "
                + "WHERE event.id=calendarevents.eventid AND calendarevents.useremail=?";

        /*String sqlQuery = "Select name, place, eventdate, type, visibility, creator from event";*/
        /*List<Event> records = new ArrayList<Event>();
        try {

            pst = con.prepareStatement(sqlQuery);
            pst.setString(1, LogUser);
            pst.execute();
            rs = pst.getResultSet();

            while (rs.next()) {
                Event event = new Event();
                event.setName(rs.getString(1));
                event.setPlace(rs.getString(2));
                event.setEventdate(rs.getString(3));
                event.setType(rs.getString(4));
                event.setVisibility(rs.getString(5));
                event.setCreator(rs.getString(6));
                event.setId(rs.getInt(7));
                records.add(event);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return records;

    }

    public Connection getConnection() {
        Connection con = null;

        String url = "jdbc:mysql://localhost:3306/nyreg";
        String user = "monty";
        String password = "some_pass";
        try {
            con = DriverManager.getConnection(url, user, password);
            System.out.println("Connection completed. ");
        } catch (SQLException ex) {
            System.out.println(ex.getMessage());
        } finally {
        }
        return con;
    } */
}


